package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyCartNPCInfo;
import com.yanqu.road.entity.beautyescort.enums.eEscortCartState;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_ROAD_MAP_ENEMY_CART, desc = "镖车情报")
public class CrossBeautyEscortEnemyCartCmd extends CrossCommand {


    long userId;
    long userServerId;
    int cartId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortEnemyCartReqMsg reqMsg = BeautyEscortProto.BeautyEscortEnemyCartReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        userServerId = reqMsg.getServerId();
        cartId = reqMsg.getCartId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossEscortPlayerWrap enemyEscortPlayer = CrossBeautyEscortMgr.getEscortPlayer(userServerId, userId);
        if(enemyEscortPlayer != null){
            BeautyEscortProto.BeautyEscortEnemyCartRespMsg.Builder respMsg = enemyEscortPlayer.getEnemyCartRespBuilder(cartId);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_ROAD_MAP_ENEMY_CART, respMsg));
        }else {
            //NPC
            BeautyEscortProto.BeautyEscortEnemyCartRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortEnemyCartRespMsg.newBuilder();
            EscortBeautyCartNPCInfo npcInfo = ConfigMgr.getEscortBeautyCartNPCInfo((int)userId);
            if(npcInfo != null){
                respMsg.setRet(0);
                respMsg.setTotalGlamour(npcInfo.getBeautyList().size() * npcInfo.getGlamour());
                respMsg.setServerId(userServerId);
                respMsg.setUserId(userId);
                respMsg.setNpc(true);
                BeautyEscortProto.BeautyEscortCartTemp.Builder cartBuilder = BeautyEscortProto.BeautyEscortCartTemp.newBuilder();
                for(int beautyId : npcInfo.getBeautyList()){
                    cartBuilder.addBeauty(CrossBeautyEscortMgr.getBeautyDispatchBuilder(beautyId, npcInfo.getGlamour()));
                }
                cartBuilder.setCartType(npcInfo.getType());
                cartBuilder.setCartId(npcInfo.getId());
                cartBuilder.setState(eEscortCartState.Escorting.getIntValue());
                cartBuilder.setEventHandle(true);
                cartBuilder.setStartTime(DateHelper.getCurrentSecond());
                respMsg.setCart(cartBuilder);
            }else {
                respMsg.setRet(GameErrorCode.E_BEAUTY_ESCORT_CART_NO_EXIST);
            }
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_ROAD_MAP_ENEMY_CART, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
